﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Windows;

namespace GrooveSharp
{

    class SizeInfo
    {
        public bool initialized = false;
        public double left, top, width, height;
    }

    class SizeManager
    {
        private SizeInfo m_compactSize = new SizeInfo();
        private SizeInfo m_normalSize = new SizeInfo();
        private bool m_isCompact = false;

        public bool IsCompact
        {
            get { return m_isCompact; }
        }

        public void SizeOrLocationChanged(Window window)
        {
            System.Diagnostics.Debug.Print("Size/location changed");

            var size = new SizeInfo();
            size.left = window.Left;
            size.top = window.Top;
            size.width = window.ActualWidth;
            size.height = window.ActualHeight;
            size.initialized = true;

            if (window.WindowState != WindowState.Maximized)
            {
                if (m_isCompact)
                {
                    System.Diagnostics.Debug.Print("-> set compact size");
                    m_compactSize = size;
                }
                else
                {
                    System.Diagnostics.Debug.Print("-> set normal size");
                    m_normalSize = size;
                }
            }
        }

        public void ToggleCompactness(Window window)
        {
            m_isCompact = !m_isCompact;
            SizeInfo size = m_isCompact ? m_compactSize : m_normalSize;

            if (!size.initialized)
            {
                // Initialize new size (before changing windows state)
                if (m_isCompact)
                {
                    // Set to lower-right
                    size.width = 180;
                    size.height = 125;
                    size.left = System.Windows.SystemParameters.PrimaryScreenWidth - size.width - 20;
                    size.top = System.Windows.SystemParameters.PrimaryScreenHeight - size.height - 30;
                }
                else
                {
                    // Center on screen
                    size.width = 500;
                    size.height = 800;
                    size.left = (System.Windows.SystemParameters.PrimaryScreenWidth - size.width) / 2.0;
                    size.top = (System.Windows.SystemParameters.PrimaryScreenHeight - size.height) / 2.0;
                }
            }

            if (m_isCompact)
            {
                window.WindowState = WindowState.Normal;
            }

            window.Left = size.left;
            window.Top = size.top;
            window.Width = size.width;
            window.Height = size.height;
        }

    }
}
